// pages/ckqx.js
const app = getApp()
Page({
  data: {
    pics: [],
    pics_2: [],
    onload: "block",
    onload_2: "block",
    haswrite: 0,
    uploadUrl: app.uploadUrl,
    shenq: false,  //审核页面
    title  :'LSamin', 
    name   : 'Lensghao',
    phone  : 13488457845,
    city   : '广东省',
    trade  : '汽车美容',
    address: '珠海市金湾区草堂湾三灶镇',
    status : 0
  },
  open_map: function () {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        that.setData({
          address: res.address,
          lat: res.latitude,
          lng: res.longitude
        })
      }
    });
  },
  //企业LOGO
  gotoShow: function () {//这里是选取图片的方法
    var that = this;
    var pics = this.data.pics;
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        wx.showLoading({
          title: '上传中',
        })
        wx.uploadFile({
          url: app.url + '/business/upload',
          filePath: res.tempFilePaths[0],
          name: 'file',
          header: { "Content-Type": "multipart/form-data" },
          success: function (ress) {
            var ress = JSON.parse(ress.data);
            if (ress.code == 1) {
              pics[0] = ress.src
              that.setData({
                pics: pics
              });
            } else {
              wx.showToast({
                title: '上传失败',
                icon: 'none'
              })
            }
            wx.hideLoading();
          }
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  deleteList(e) {
    const index = e.currentTarget.dataset.index;
    let pics = this.data.pics;
    pics.splice(index, 1);
    this.setData({
      pics: pics
    });
    if (pics.length < 4) {
      this.setData({
        onload: "block",
      });
    }
  },
  // 营业执照
  gotoShow_2: function () {//这里是选取图片的方法
    var that = this;
    var pics_2 = this.data.pics_2;
    wx.chooseImage({
      count: 3, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        wx.showLoading({ title: '上传中', })
        wx.uploadFile({
          url: app.url + '/business/upload',
          filePath: res.tempFilePaths[0],
          name: 'file',
          header: { "Content-Type": "multipart/form-data" },
          success: function (e) {
            if (pics_2.length < 3) {
              that.setData({ onload: "", });
              console.log(JSON.parse(e.data).src);
              pics_2.push(JSON.parse(e.data).src)
              that.setData({ pics_2: pics_2 })
            } else {
              wx.hideLoading();
              that.setData({ onload: "none", });
              return false;
            }
            wx.hideLoading();
          }
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  deleteList_2(e) {
    const index = e.currentTarget.dataset.index;
    let pics_2 = this.data.pics_2;
    pics_2.splice(index, 1);
    this.setData({ pics_2: pics_2 });
    if (pics_2.length < 4) { this.setData({ onload_2: "block", }); }
  },
  // input
  input: function (e) {
    console.log(e)
    let that = this
    var haswrite = that.data.haswrite
    if (e.detail.value !== "" && haswrite < 6) {
      that.setData({ haswrite: haswrite + 1 })
    }
    else if (haswrite > 0) {
      that.setData({ haswrite: haswrite - 1 })
    }
    // console.log(haswrite,"--------------------------------")
  },
  // 提交申请
  formSubmit: function (res) {
    var that = this;
    var res = res.detail.value;
    if (that.data.pics.length < 1) {
      wx.showToast({ title: '请上传logo图片', icon: 'none' }); return false;
    }
    if (that.data.pics_2.length < 2) {
      wx.showToast({ title: '请上传营业执照正反面', icon: 'none' }); return false;
    }
    let address = that.data.address;
    let lat = that.data.lat;
    let lng = that.data.lng;
    let logo = that.data.pics[0];
    let licence = that.data.pics_2.join();
    let name = res.name;
    let username = res.username;
    let phone = res.phone
    let trade = res.trade
    if (!address || !lat || !lng) {
      wx.showToast({ title: '请选择地址', icon: 'none' }); return false;
    }
    if (!(/^1[3|4|5|6|8|7][0-9]\d{4,8}$/.test(phone))) {
      wx.showToast({ title: '手机号码错误', icon: 'none' }); return false;
    }
    if (!name || !username || !trade) {
      wx.showToast({ title: '信息填写不完整', icon: 'none' }); return false;
    }
    wx.request({
      url: app.url + '/Business/verify_edit',
      method: 'post',
      data: {
        uid: 6, address, lat, lng, logo, licence, name, username, phone, trade
      },
      success: function (res) {
        var code = res.data.code;
        if (code == 1) {
          wx.showModal({
            title: '提示', content: '提交成功，请耐心等待',
            success: function (result) {
              if (result.confirm || result.cancel) { wx.navigateBack({ delta: 1 }) }
            }
          })
        } else if (code == 2000) {
          wx.showToast({ title: '用户未登录', icon: 'none' })
        } else if (code == 2001) {
          wx.showToast({ title: '未获取到位置信息', icon: 'none' })
        } else if (code == 2002) {
          wx.showToast({ title: '入驻提交失败', icon: 'none' })
        } else {
          wx.showToast({ title: '您已入驻', icon: 'none' })
        }
      }
    })
  },
  onLoad: function (options) {
    var that = this;
    wx.request({
      url  : app.url +'/Business/auditing_detail',
      data : {
        uid:wx.getStorageSync('uid')
      },
      method  : 'post',
      success : function(res){
        if(res.data.code == 1){
          var resp   = res.data.data;
          var pics   = [],logo  = [];
          var licence= resp.licence.split(',');
          logo.push({ pics: app.uploadUrl + resp.logo });
          for( var i = 0 ;i < licence.length ;i++){
            pics.push({ pics : app.uploadUrl + licence[i]})
          } 
          that.setData({
            title  : resp.title, 
            name   : resp.name, 
            phone  : resp.phone, 
            city   : resp.city,
            address: resp.address, 
            logo   : logo,
            licence: pics,
            trade　: resp.trade
          })
        }
     }
   })
  },

  onReady: function () {
  
  },

  onShow: function () {
  
  },

  onHide: function () {
  
  },

  onUnload: function () {
  
  },

  onPullDownRefresh: function () {
  
  },

  onReachBottom: function () {
  
  },

  onShareAppMessage: function () {
  
  }
})